package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.gourdchild.GourdChildConfig;
import com.yanqu.road.entity.activity.gourdchild.GourdChildUserData;
import com.yanqu.road.entity.activity.gourdchild.Grid;
import com.yanqu.road.entity.activity.gourdchild.GridData;
import com.yanqu.road.entity.activity.gourdchild.config.GourdLevelBookInfo;
import com.yanqu.road.entity.activity.gourdchild.config.GourdLevelInfo;
import com.yanqu.road.entity.activity.gourdchild.config.GourdLevelLotusInfo;
import com.yanqu.road.entity.activity.gourdchild.enums.eElementType;
import com.yanqu.road.pb.activity.GourdChildProto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GourdChildPb {


    /**
     * 配置
     */
    public static GourdChildProto.GourdChildSyncConfigRespMsg.Builder parseSyncConfigRespMsg(GourdChildConfig config, String language) {
        GourdChildProto.GourdChildSyncConfigRespMsg.Builder builder = GourdChildProto.GourdChildSyncConfigRespMsg.newBuilder();
        for (Map.Entry<Integer, GourdLevelInfo> entry : config.getLevelInfoMap().entrySet()) {
            builder.addLevel(parseGourdChildLevelConfigTemp(entry.getValue(), language));
        }
        for (Map.Entry<Integer, GourdLevelBookInfo> infoEntry : config.getBookInfoMap().entrySet()) {
            builder.addBook(parseGourdChildBookConfigTemp(infoEntry.getValue(), language));
        }
        for (Map.Entry<Integer, GourdLevelLotusInfo> entry : config.getLotusInfoMap().entrySet()) {
            builder.addLotus(parseGourdChildLotusConfigTemp(entry.getValue(), language));
        }
        return builder;
    }

    /**
     * 彩莲
     */
    public static GourdChildProto.GourdChildLotusConfigTemp.Builder parseGourdChildLotusConfigTemp(GourdLevelLotusInfo lotusInfo, String language){
        GourdChildProto.GourdChildLotusConfigTemp.Builder builder = GourdChildProto.GourdChildLotusConfigTemp.newBuilder();
        builder.setLevel(lotusInfo.getLevel());
        builder.setDamage(lotusInfo.getDamage());
        builder.setConsume(lotusInfo.getConsume());
        return builder;
    }


    /**
     * 葫芦图鉴
     */
    public static GourdChildProto.GourdChildBookConfigTemp.Builder parseGourdChildBookConfigTemp(GourdLevelBookInfo bookInfo, String language){
        GourdChildProto.GourdChildBookConfigTemp.Builder builder = GourdChildProto.GourdChildBookConfigTemp.newBuilder();
        builder.setBookId(bookInfo.getBookId());
        builder.setShowProbability(bookInfo.getShowProbability());
        builder.setShowReward(bookInfo.getShowReward());
        builder.setIsHide(bookInfo.getIsHide());
        builder.setSort(bookInfo.getSort());
        return builder;
    }

    /**
     * 关卡
     */
    public static GourdChildProto.GourdChildLevelConfigTemp.Builder parseGourdChildLevelConfigTemp(GourdLevelInfo levelInfo, String language){
        GourdChildProto.GourdChildLevelConfigTemp.Builder builder = GourdChildProto.GourdChildLevelConfigTemp.newBuilder();
        builder.setId(levelInfo.getId());
        builder.setType(levelInfo.getType());
        builder.setChapter(levelInfo.getChapter());
        builder.setLevel(levelInfo.getLevel());
        builder.setMonster(levelInfo.getMonster());
        builder.addAllStageType(levelInfo.getStageType());
        builder.setMaxRound(levelInfo.getMaxRound());
        builder.setAdviceDamage(levelInfo.getAdviceDamage());
        builder.setEnergy(levelInfo.getEnergy());
        builder.setReward(levelInfo.getReward());
        builder.setDefeatReward(levelInfo.getDefeatReward());
        builder.setHp(levelInfo.getHp());
        builder.addAllStarLimit(levelInfo.getStarLimit());
        builder.setEnergy(levelInfo.getEnergy());
        builder.setRandomReward(levelInfo.getRandomReward());
        builder.addAllElement(levelInfo.getElement());
        return builder;
    }

    /**
     * 游戏数据
     */
    public static GourdChildProto.GourdChildGameDataMsg.Builder parseGameDataMsg(GourdChildUserData userData, GourdChildConfig childConfig){
        GourdChildProto.GourdChildGameDataMsg.Builder builder = GourdChildProto.GourdChildGameDataMsg.newBuilder();
        //
        GourdLevelLotusInfo levelLotusInfo = childConfig.getLotusInfoMap().get(userData.getLotusLv());
        builder.setBaseDamage(levelLotusInfo.getDamage());
        GourdChildProto.GourdChildPassDataTemp.Builder pass = GourdChildProto.GourdChildPassDataTemp.newBuilder();
        pass.setRound(userData.getPassDetail().getRound());
        pass.setPassId(userData.getPassDetail().getPassId());
        pass.setBlood(userData.getPassDetail().getBlood());
        if(userData.getPassDetail().getPassId() > 0){
            GourdLevelInfo levelInfo = childConfig.getLevelInfoMap().get(userData.getPassDetail().getPassId());
            pass.setHp(levelInfo.getHp());
            pass.setMonsterId(levelInfo.getMonster());
        }else {
            pass.setHp(0);
            pass.setMonsterId(0);
        }
        builder.setPass(pass);

        GourdChildProto.GourdChildGridTemp.Builder next = GourdChildProto.GourdChildGridTemp.newBuilder();
        for (GridData gridData : userData.getNextGrid().getData()) {
            GourdChildProto.GourdChildGridDataTemp.Builder gridDataBuild = GourdChildProto.GourdChildGridDataTemp.newBuilder();
            gridDataBuild.setElement(gridData.getEt() == eElementType.ELEMENT.getType());
            gridDataBuild.setLv(gridData.getLv());
            gridDataBuild.setType(gridData.getOt());
            next.addGridData(gridDataBuild);
        }
        builder.setNext(next);

        builder.setHistoryMaxPass(userData.getHistoryPassId());
        builder.setEnergy(userData.getEnergy());
        builder.setLastRecTime(userData.getLastRecTime());
        builder.setLotusLv(userData.getLotusLv());
        builder.setPassId(userData.getPassId());
        builder.setScore(userData.getScore());
        builder.setNormalDamage(userData.getNormalDamage());
        builder.setTrainDamage(userData.getTrainDamage());
        List<GourdChildProto.GourdChildGridTemp> gridTempList = getGourdChildGridTemps(userData.getGridList());
        builder.addAllGrid(gridTempList);

        List<GourdChildProto.GourdChildPassStarTemp> starTempList = getGourdChildPassStarTemps(userData);
        builder.addAllStar(starTempList);

        return builder;
    }



    /**
     * 游戏数据（练习）
     */
    public static GourdChildProto.GourdChildGameDataMsg.Builder parseTrainGameDataMsg(GourdChildUserData userData, GourdChildConfig childConfig){
        GourdChildProto.GourdChildGameDataMsg.Builder builder = GourdChildProto.GourdChildGameDataMsg.newBuilder();
        //
        GourdLevelLotusInfo levelLotusInfo = childConfig.getLotusInfoMap().get(userData.getLotusLv());
        builder.setBaseDamage(levelLotusInfo.getDamage());
        GourdChildProto.GourdChildPassDataTemp.Builder pass = GourdChildProto.GourdChildPassDataTemp.newBuilder();
        pass.setRound(userData.getTempPassDetail().getRound());
        pass.setPassId(userData.getTempPassDetail().getPassId());
        pass.setBlood(userData.getTempPassDetail().getBlood());
        if(userData.getTempPassDetail().getPassId() > 0){
            GourdLevelInfo levelInfo = childConfig.getLevelInfoMap().get(userData.getTempPassDetail().getPassId());
            pass.setHp(levelInfo.getHp());
            pass.setMonsterId(levelInfo.getMonster());
        }else {
            pass.setHp(0);
            pass.setMonsterId(0);
        }
        builder.setPass(pass);

        GourdChildProto.GourdChildGridTemp.Builder next = GourdChildProto.GourdChildGridTemp.newBuilder();
        for (GridData gridData : userData.getTempNextGrid().getData()) {
            GourdChildProto.GourdChildGridDataTemp.Builder gridDataBuild = GourdChildProto.GourdChildGridDataTemp.newBuilder();
            gridDataBuild.setElement(gridData.getEt() == eElementType.ELEMENT.getType());
            gridDataBuild.setLv(gridData.getLv());
            gridDataBuild.setType(gridData.getOt());
            next.addGridData(gridDataBuild);
        }
        builder.setNext(next);

        builder.setEnergy(userData.getEnergy());
        builder.setLastRecTime(userData.getLastRecTime());
        builder.setLotusLv(userData.getLotusLv());
        builder.setPassId(userData.getPassId());
        builder.setScore(userData.getScore());
        builder.setNormalDamage(userData.getNormalDamage());
        builder.setTrainDamage(userData.getTrainDamage());
        List<GourdChildProto.GourdChildGridTemp> gridTempList = getGourdChildGridTemps(userData.getTempGridList());
        builder.addAllGrid(gridTempList);

        List<GourdChildProto.GourdChildPassStarTemp> starTempList = getGourdChildPassStarTemps(userData);
        builder.addAllStar(starTempList);
        return builder;
    }

    public static List<GourdChildProto.GourdChildPassStarTemp> getGourdChildPassStarTemps(GourdChildUserData userData) {
        List<GourdChildProto.GourdChildPassStarTemp> starTempList = new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : userData.getPassStar().entrySet()) {
            GourdChildProto.GourdChildPassStarTemp.Builder starBuilder = GourdChildProto.GourdChildPassStarTemp.newBuilder();
            starBuilder.setPassId(entry.getKey());
            starBuilder.setStar(entry.getValue());
            starTempList.add(starBuilder.build());
        }
        return starTempList;
    }

    public static List<GourdChildProto.GourdChildGridTemp> getGourdChildGridTemps(List<Grid> gridList) {
        List<GourdChildProto.GourdChildGridTemp> gridTempList = new ArrayList<>();
        for (Grid grid : gridList) {
            GourdChildProto.GourdChildGridTemp.Builder builder1 = GourdChildProto.GourdChildGridTemp.newBuilder();
            for (GridData gridData : grid.getData()) {
                GourdChildProto.GourdChildGridDataTemp.Builder gridDataBuild = GourdChildProto.GourdChildGridDataTemp.newBuilder();
                gridDataBuild.setElement(gridData.getEt() == eElementType.ELEMENT.getType());
                gridDataBuild.setLv(gridData.getLv());
                gridDataBuild.setType(gridData.getOt());
                builder1.addGridData(gridDataBuild);
            }
            gridTempList.add(builder1.build());
        }
        return gridTempList;
    }
}
